﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SKN
{
    /// <summary>
    /// Contains common method for Site Collection
    /// </summary>
    public static class SiteCollection
    {
        /// <summary>
        /// Use this method to quickly provision a new site collection
        /// </summary>
        /// <param name="webAppUrl"></param>
        /// <param name="strUrl"></param>
        /// <param name="strTitle"></param>
        /// <param name="strDesc"></param>
        /// <param name="nLcid"></param>
        /// <param name="strTemplate"></param>
        /// <param name="strOwnerLogin1"></param>
        /// <param name="strOwnerName1"></param>
        /// <param name="strOwnerEmail1"></param>
        /// <param name="strOwnerLogin2"></param>
        /// <param name="strOwnerName2"></param>
        /// <param name="strOwnerEmail2"></param>
        /// <param name="hhMode"></param>
        public static void CreateSiteCollection(string webAppUrl, string strUrl, string strTitle, string strDesc,
                                                uint nLcid, string strTemplate,
                                                string strOwnerLogin1, string strOwnerName1, string strOwnerEmail1,
                                                string strOwnerLogin2, string strOwnerName2, string strOwnerEmail2,
                                                bool hhMode)
        {
            var srvrUri = new Uri(webAppUrl);
            SPWebApplication webApp = SPWebApplication.Lookup(srvrUri);

            SPSiteCollection siteColl = webApp.Sites;
            SPSite oSite = null;
            try
            {
                // If the secondary owner fields are empty, convert them to null
                if (String.IsNullOrEmpty(strOwnerLogin2)) strOwnerLogin2 = null;
                if (String.IsNullOrEmpty(strOwnerName2)) strOwnerName2 = null;
                if (String.IsNullOrEmpty(strOwnerEmail2)) strOwnerEmail2 = null;

                // elevate permissions to allow user to create a new site.
                SPSecurity.RunWithElevatedPrivileges(delegate
                                                         {
                                                             // add site to the site collection
                                                             oSite = siteColl.Add(strUrl, strTitle, strDesc, nLcid,
                                                                                  strTemplate,
                                                                                  strOwnerLogin1, strOwnerName1,
                                                                                  strOwnerEmail1,
                                                                                  strOwnerLogin2, strOwnerName2,
                                                                                  strOwnerEmail2, true);
                                                         });
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                // dispose object
                if (oSite != null)
                    oSite.Dispose();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="webAppUrl"></param>
        /// <param name="strSiteUrl"></param>
        /// <param name="bDeleteUsers"></param>
        public static void DeleteSiteCollection(string webAppUrl, string strSiteUrl, bool bDeleteUsers)
        {
            var srvrUri = new Uri(webAppUrl);
            SPWebApplication webApp = SPWebApplication.Lookup(srvrUri);

            SPSiteCollection siteColl = webApp.Sites;
            string[] siteUrls = siteColl.Names;
            // iterate through the site collection to make sure we're deleting a site that exists.
            foreach (string url in siteUrls)
            {
                if (string.Equals(url,strSiteUrl,StringComparison.InvariantCultureIgnoreCase))
                {
                    // elevate permissions to allow user to delete a site.
                    SPSecurity.RunWithElevatedPrivileges(delegate
                                                             {
                                                                 // delete site from the site collection
                                                                 siteColl.Delete(url, bDeleteUsers);
                                                             });
                }
            }
        }
    }
}